/**
 * @TcpClientMain.java  COPYRIGHT xiaojian
 * 
 * Created on 2017年6月23日 下午5:38:29
 */
package xj.toolkit.benchmark.test.tcp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import xj.toolkit.bean.bytebean.core.BeanFieldCodec;
import xj.toolkit.bean.util.meta.Int2TypeMetainfo;
import xj.toolkit.benchmark.bean.GetUrlReq;
import xj.toolkit.netty.handler.codec.tcp.decode.MixedXipDecoder;
import xj.toolkit.netty.handler.codec.tcp.encode.MixedXipEncoder;
import xj.toolkit.util.UUIDIdentifier;

/**
 * @author <a href="mailto:cxj2000@gmail.com">xiaojian.cao</a>
 *
 */
public class TcpClientMain {

	private static final Logger logger = LoggerFactory.getLogger(TcpClientMain.class);
	
    public static String HOST = "127.0.0.1";
    public static int PORT = 4111;

    /**
     * 初始化Bootstrap
     * 
     * @return
     */
    public static void main(String[] args) throws Exception {
    	
    	AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
				"xipDemo2.xml");

		BeanFieldCodec xipBeanCodec = (BeanFieldCodec) ctx.getBean("beanCodec");
		Int2TypeMetainfo xipTypeMetainfo = ctx.getBean(Int2TypeMetainfo.class);
		
		final MixedXipDecoder decoder = new MixedXipDecoder();
		decoder.setDebugEnabled(true);
		decoder.setXipBeanCodec(xipBeanCodec);
		decoder.setXipTypeMetainfo(xipTypeMetainfo);
		
		final MixedXipEncoder encoder = new MixedXipEncoder();
		encoder.setXipBeanCodec(xipBeanCodec);
    	
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class);
        bootstrap.handler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast("decoder",decoder);
                pipeline.addLast("encoder",encoder);
                pipeline.addLast("handler", new TcpClientHandler());
            }
        });
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);

        Channel channel = null;
        try {
            channel = bootstrap.connect(HOST, PORT).sync().channel();
        } catch (Exception e) {
            logger.error(
                    String.format("连接Server(IP[%s],PORT[%s])失败", HOST, PORT), e);
        }
        
        Thread.sleep(1 * 1000);
    	
        GetUrlReq req = new GetUrlReq();
        req.setIdentification(new UUIDIdentifier());
        req.setName("100");
        
        logger.debug("req->" + req);
        
        channel.writeAndFlush(req);
        
        Thread.sleep(500 * 1000);
        
        ctx.close();
    }



}
